package com.raymond.http;

import com.raymond.http.netty.HttpNetty;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.pool.FixedChannelPool;
import io.netty.handler.codec.http.*;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.FutureListener;
import lombok.extern.slf4j.Slf4j;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-12-11 15:41
 */
@Slf4j
public class NettyClientTest {
    public static void main(String[] args) throws MalformedURLException, URISyntaxException, InterruptedException {
        urlTest();
        HttpNettyClient httpNettyClient = new HttpNettyClient();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            scanner.next();
            test(httpNettyClient);
        }
    }

    public static void test(HttpNettyClient httpNettyClient) throws InterruptedException {
        String url = "http://192.169.1.129:8088/sms/v2/std/send_batch";
        String url1 = "http://192.169.1.129:8087/sms/v2/std/send_batch";
        String message = "{\"mobile\":\"17659485948\",\"pwd\":\"6ab866b831640f2fab0d9f5de539d317\",\"timestamp\":\"1216100927\",\"userid\":\"ZSY001\"}";
        int threadCount = 1;
        final CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        final AtomicInteger atomicInteger = new AtomicInteger();
        long start = System.currentTimeMillis();
        for (int i = 0; i < threadCount; i++) {
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for (; atomicInteger.get() < 1000000; atomicInteger.incrementAndGet()) {
                            DefaultFullHttpRequest request = getHttpRequest(atomicInteger.get() % 2 == 0 ? url : url1, message);

                            httpNettyClient.execute(request);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    countDownLatch.countDown();
                }
            });

            thread.start();
        }
        countDownLatch.await();


//        for (int i = 0; i < 10000; i++) {
//            try {
//                DefaultFullHttpRequest request = getHttpRequest(url, message);
//                httpNettyClient.execute(request);
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }

        log.debug("请求总耗时:{}", (System.currentTimeMillis() - start));
    }


    public static DefaultFullHttpRequest getHttpRequest(String url, String message) {
        DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, url,
                Unpooled.wrappedBuffer(message.getBytes(StandardCharsets.UTF_8)), true);
        request.headers().set(HttpHeaderNames.CONTENT_LENGTH, request.content().readableBytes());
        return request;
    }

    public static void urlTest() throws MalformedURLException, URISyntaxException {
        URL url2 = new URL("http://127.0.0.1:8080/emp");
        System.out.println(url2.getPath());
        System.out.println(url2.getHost());
        URI url = new URI("/emp");
        System.out.println(url.getHost());
        System.out.println(url.getPort());
        URI url1 = new URI("https://127.0.0.1:8080/ump");
        System.out.println(url.equals(url1));
        System.out.println(url1.getScheme());
    }
}
